package com.tbit.uqbike.client.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 年例工具类
 *
 * @author Leon
 * 2017年8月3日 下午2:18:31
 */
public class AgeUtil {
    /**
     * 根据身份证号码获取年龄
     *
     * @param IdNO
     * @return
     */
    public static int IdNOToAge(String IdNO) {
        int leh = IdNO.length();
        String dates;
        if (leh == 18) {
            dates = IdNO.substring(6, 10);
            SimpleDateFormat df = new SimpleDateFormat("yyyy");
            String year = df.format(new Date());
            return Integer.parseInt(year) - Integer.parseInt(dates);
        } else {
            dates = IdNO.substring(6, 8);
            return Integer.parseInt(dates);
        }
    }

    /**
     * 检验是否满 num 周岁
     *
     * @param cardId 身份证
     * @param num    岁数
     * @return
     */
    public static boolean checkIsMan(String cardId, Integer num) {
        String birthYear = cardId.substring(6, 10);
        String birthMonth = cardId.substring(10, 12);
        String birthDay = cardId.substring(12, 14);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String date = sdf.format(new Date());
        /**年*/
        String year = date.substring(0, 4);
        /**月*/
        String month = date.substring(4, 6);
        /**日*/
        String day = date.substring(6, 8);
        int age = Integer.valueOf(year) - Integer.valueOf(birthYear);
        if (age > num) {
            return true;
        } else {
            if (age < num) {
                return false;
            } else {
                if (Integer.valueOf(birthMonth) > Integer.valueOf(month)) {
                    return false;
                } else if (Integer.valueOf(birthMonth) < Integer.valueOf(month)) {
                    return true;
                } else {
                    return Integer.valueOf(birthDay) <= Integer.valueOf(day);
                }
            }
        }
    }

}
